home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / UUPC3 / MAC_SPEC / UNIX_LIB / GETCWD.C < prev    next >
C/C++ Source or Header  |  1991-12-02  |  2KB  |  111 lines

  1. #ifdef THINK_C
  2. # include "unixlibproto.h"
  3. #endif THINK_C
  4.  
  5. #ifndef    THINK_C
  6. #include <memory.h>
  7. #include <aztec/shell.h>
  8.  
  9. #ifdef TEST
  10. #include <stdio.h>
  11. #define _DEBUG
  12. #include <max/debug.h>
  13. #endif
  14. #endif    THINK_C
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include "host.h"
  19.  
  20. #ifndef NULL
  21. #define NULL 0L
  22. #endif
  23.  
  24. char * getcwd( path, size )
  25. char * path;
  26. int size;
  27. {
  28.  
  29.     static        char    name[255];
  30.     char        *p, *dir;
  31.     OSErr        rc;
  32.     WDPBRec        wd;
  33.  
  34. #ifdef THINK_C
  35.     memset((char *)(&wd), (int)NULL, (size_t)sizeof(wd));
  36. #else THINK_C
  37.     repmem((char *)&wd, "", 1, sizeof(wd));
  38. #endif THINK_C
  39.     
  40.     if (PBHGetVol(&wd, false) != noErr) {
  41.         return NULL;
  42.     }
  43.  
  44.     if (av_getname(name, wd.ioWDDirID)) {
  45.         return NULL;
  46.     }
  47.     if (*(name+strlen(name)-1) == DIRCHAR) *(name+strlen(name)-1) = '\0';
  48.     /* convert to Unix style name */
  49.     dir = name;
  50.     if ((p = index(name, DIRCHAR)) == NULL) p = name;
  51.     else {
  52.         dir = p;
  53.         *p++ = SEPCHAR;        /* remove vol name and inser leading / */
  54.     }
  55.     /* replace all : with / */
  56.     while (*p) {
  57.         if (*p == DIRCHAR) *p = SEPCHAR;
  58.         p++;
  59.     }
  60.     if (path != NULL) {
  61.         if (strlen(dir) > size) return(NULL);
  62.         strcpy(path, dir);
  63.         return path;
  64.     }
  65.     else {
  66.         return(dir);
  67.     }
  68. }
  69.  
  70. int av_getname(name, n)
  71. char *name;
  72. long n;
  73. {
  74.     CInfoPBRec    pb;
  75.     OSErr        rc;
  76.     char        space[32];
  77.  
  78. #ifdef THINK_C
  79.         memset((char *)(&pb), (int)NULL, (size_t)sizeof(pb));
  80. #else THINK_C
  81.         repmem((char *)&pb, "", 1, sizeof(pb));
  82. #endif THINK_C
  83.     
  84.     pb.dirInfo.ioNamePtr    = (StringPtr)space;
  85.     pb.dirInfo.ioFDirIndex  = -1;
  86.     pb.dirInfo.ioDrDirID    = n;
  87.         
  88.     if (PBGetCatInfo(&pb, FALSE) != noErr) {
  89.         return -1;
  90.     }
  91.     PtoCstr(space);
  92.     if (n <= 2) {
  93.         strcpy(name, space);
  94.     } else {
  95.         av_getname(name, pb.dirInfo.ioDrParID);
  96.         strcat(name, ":");
  97.         strcat(name, space);
  98.     }
  99.     return 0;
  100. }
  101.  
  102. #ifdef TEST
  103. main()
  104. {
  105.  
  106.     fprintf( stderr, "%s\n", getcwd( NULL, 0 ));
  107. }
  108.  
  109. #endif
  110.  
  111.